// 奥数学院题库索引 - 汇总所有题目
import { 鸡兔同笼_QUESTIONS } from './primary-school/鸡兔同笼'
import { 和差倍问题_QUESTIONS } from './primary-school/和差倍问题'
import { 年龄问题_QUESTIONS } from './primary-school/年龄问题'
import { 平均数问题_QUESTIONS } from './primary-school/平均数问题'
import { 行程问题_QUESTIONS } from './primary-school/行程问题'
import { 工程问题_QUESTIONS } from './primary-school/工程问题'
import { 盈亏问题_QUESTIONS } from './primary-school/盈亏问题'
import { 植树问题_QUESTIONS } from './primary-school/植树问题'
import { 数论基础_QUESTIONS } from './primary-school/数论基础'
import { 几何计算_QUESTIONS } from './primary-school/几何计算'
import { 计数原理_QUESTIONS } from './primary-school/计数原理'
import { 分数应用题_QUESTIONS } from './primary-school/分数应用题'
import { 小数应用题_QUESTIONS } from './primary-school/小数应用题'
import { 百分数应用题_QUESTIONS } from './primary-school/百分数应用题'
import { 比例应用题_QUESTIONS } from './primary-school/比例应用题'
import { 一元一次方程_QUESTIONS } from './middle-school/一元一次方程'
import { 二元一次方程组_QUESTIONS } from './middle-school/二元一次方程组'
import { 二次函数_QUESTIONS } from './middle-school/二次函数'
import { 相似三角形_QUESTIONS } from './middle-school/相似三角形'

import 容斥原理_QUESTIONS from './primary-school/容斥原理'
import { 逻辑推理_QUESTIONS } from './primary-school/逻辑推理'
import 抽屉原理_QUESTIONS from './primary-school/抽屉原理'
import 数字谜题_QUESTIONS from './primary-school/数字谜题'
import { 周期问题_QUESTIONS } from './primary-school/周期问题'
import { 归一问题_QUESTIONS } from './primary-school/归一问题'
import { 牛吃草问题_QUESTIONS } from './primary-school/牛吃草问题'
import { 时钟问题_QUESTIONS } from './primary-school/时钟问题'
import 日历问题_QUESTIONS from './primary-school/日历问题'
import 图形计数_QUESTIONS from './primary-school/图形计数'
import 找规律_QUESTIONS from './primary-school/找规律'
import 数列问题_QUESTIONS from './primary-school/数列问题'
import 面积计算_QUESTIONS from './primary-school/面积计算'
import 体积计算_QUESTIONS from './primary-school/体积计算'
import 空间想象_QUESTIONS from './primary-school/空间想象'
import 概率统计_QUESTIONS from './primary-school/概率统计'
import 排列组合_QUESTIONS from './primary-school/排列组合'
import 数学游戏_QUESTIONS from './primary-school/数学游戏'
import 不等式_QUESTIONS from './primary-school/不等式'
import 函数基础_QUESTIONS from './primary-school/函数基础'

// 新增初中数学题库
import { 几何证明_QUESTIONS } from './middle-school/几何证明';
import { 函数综合_QUESTIONS } from './middle-school/函数综合';
import { 圆的性质_QUESTIONS } from './middle-school/圆的性质';
import { 解直角三角形_QUESTIONS } from './middle-school/解直角三角形';
import { 统计与概率_QUESTIONS } from './middle-school/统计与概率';
import { 反比例函数_QUESTIONS } from './middle-school/反比例函数';
import { 代数综合_QUESTIONS } from './middle-school/代数综合';
import { 几何综合_QUESTIONS } from './middle-school/几何综合';
import { 应用题综合_QUESTIONS } from './middle-school/应用题综合';
import { 动点问题_QUESTIONS } from './middle-school/动点问题';
import { 中考专题_QUESTIONS } from './middle-school/中考专题';
import { 数据分析_QUESTIONS } from './middle-school/数据分析';
import { 方差标准差_QUESTIONS } from './middle-school/方差标准差';
import { 回归分析_QUESTIONS } from './middle-school/回归分析';
import { 竞赛基础_QUESTIONS } from './middle-school/竞赛基础';
import { 数学建模_QUESTIONS } from './middle-school/数学建模';
import { 实际应用_QUESTIONS } from './middle-school/实际应用';
import { 综合压轴题_QUESTIONS } from './middle-school/综合压轴题';

// 题目接口定义
export interface Question {
  stem: string;        // 题目描述
  difficulty: number;  // 难度等级（1~5）
  answer: any;         // 标准答案
  hint1?: string;      // 第一个提示
  hint2?: string;      // 第二个提示
  solution: string;    // 详细解析
}

// 英文ID到中文名称的映射
export const TOPIC_ID_TO_NAME: { [key: string]: string } = {
  'chicken-rabbit-cage': '鸡兔同笼',
  'sum-difference-multiple': '和差倍问题',
  'age-problem': '年龄问题',
  'average-problem': '平均数问题',
  'distance-problem': '行程问题',
  'meeting-problem': '行程问题',
  'chase-problem': '行程问题',
  'circular-problem': '行程问题',
  'water-current-problem': '行程问题',
  'work-problem': '工程问题',
  'profit-loss-problem': '盈亏问题',
  'tree-planting': '植树问题',
  'number-theory': '数论基础',
  'prime-number': '数论基础',
  'factor-multiple': '数论基础',
  'odd-even-property': '数论基础',
  'geometry-calculation': '几何计算',
  'area-perimeter-problem': '面积计算',
  'volume-problem': '体积计算',
  'counting-principle': '计数原理',
  'fraction-application': '分数应用题',
  'decimal-application': '小数应用题',
  'percentage-application': '百分数应用题',
  'concentration-problem': '百分数应用题',
  'profit-problem': '百分数应用题',
  'ratio-application': '比例应用题',
  'inclusion-exclusion': '容斥原理',
  'logical-reasoning': '逻辑推理',
  'pigeonhole-principle': '抽屉原理',
  'number-puzzle': '数字谜题',
  'period-problem': '周期问题',
  'unit-rate-problem': '归一问题',
  'cow-grass-problem': '牛吃草问题',
  'clock-problem': '时钟问题',
  'calendar-problem': '日历问题',
  'shape-counting': '图形计数',
  'figure-counting': '图形计数',
  'pattern-finding': '找规律',
  'sequence-problem': '数列问题',
  'recursive-problem': '数列问题',
  'area-calculation': '面积计算',
  'volume-calculation': '体积计算',
  'spatial-imagination': '空间想象',
  'probability-statistics': '概率统计',
  'permutation-combination': '排列组合',
  'math-game': '数学游戏',
  'magic-square-problem': '数学游戏',
  'inequality': '不等式',
  'optimization-problem': '不等式',
  'function-basics': '函数基础',
  'function-thinking': '函数基础',
  'coordinate-problem': '函数基础',
  'linear-equation': '一元一次方程',
  'linear-equations': '一元一次方程',
  'linear-inequalities': '不等式',
  'linear-system': '二元一次方程组',
  'binary-linear-equations': '二元一次方程组',
  'quadratic-function': '二次函数',
  'quadratic-functions': '二次函数',
  'linear-functions': '函数基础',
  'similar-triangles': '相似三角形',
  'triangle-properties': '相似三角形',
  'similarity-congruence': '相似三角形',
  'geometry-proof': '几何证明',
  'geometric-proof': '几何证明',
  'pythagorean-theorem': '解直角三角形',
  'quadrilateral-properties': '几何综合',
  'function-comprehensive': '函数综合',
  'circle-properties': '圆的性质',
  'right-triangle': '解直角三角形',
  'statistics-probability': '统计与概率',
  'statistics-analysis': '数据分析',
  'advanced-probability': '概率统计',
  'inverse-proportion': '反比例函数',
  'inverse-proportional-functions': '反比例函数',
  'divisibility-rules': '数论基础',
  'advanced-counting': '排列组合',
  'algebra-comprehensive': '代数综合',
  'geometry-comprehensive': '几何综合',
  'application-comprehensive': '应用题综合',
  'moving-point': '动点问题',
  'exam-special': '中考专题',
  'data-analysis': '数据分析',
  'variance-std': '方差标准差',
  'regression-analysis': '回归分析',
  'competition-basics': '竞赛基础',
  'math-modeling': '数学建模',
  'practical-application': '实际应用',
  'comprehensive-finale': '综合压轴题',
};

// 知识点题目映射
export const QUESTION_BANK: { [key: string]: Question[] } = {
  // 小学奥数题型
  '鸡兔同笼': 鸡兔同笼_QUESTIONS,
  '和差倍问题': 和差倍问题_QUESTIONS,
  '年龄问题': 年龄问题_QUESTIONS,
  '平均数问题': 平均数问题_QUESTIONS,
  '行程问题': 行程问题_QUESTIONS,
  '工程问题': 工程问题_QUESTIONS,
  '盈亏问题': 盈亏问题_QUESTIONS,
  '植树问题': 植树问题_QUESTIONS,
  '数论基础': 数论基础_QUESTIONS,
  '几何计算': 几何计算_QUESTIONS,
  '计数原理': 计数原理_QUESTIONS,
  '分数应用题': 分数应用题_QUESTIONS,
  '小数应用题': 小数应用题_QUESTIONS,
  '百分数应用题': 百分数应用题_QUESTIONS,
  '比例应用题': 比例应用题_QUESTIONS,
  '容斥原理': 容斥原理_QUESTIONS,
  '逻辑推理': 逻辑推理_QUESTIONS,
  '抽屉原理': 抽屉原理_QUESTIONS,
  '数字谜题': 数字谜题_QUESTIONS,
  '周期问题': 周期问题_QUESTIONS,
  '归一问题': 归一问题_QUESTIONS,
  '牛吃草问题': 牛吃草问题_QUESTIONS,
  '时钟问题': 时钟问题_QUESTIONS,
  '日历问题': 日历问题_QUESTIONS,
  '图形计数': 图形计数_QUESTIONS,
  '找规律': 找规律_QUESTIONS,
  '数列问题': 数列问题_QUESTIONS,
  '面积计算': 面积计算_QUESTIONS,
  '体积计算': 体积计算_QUESTIONS,
  '空间想象': 空间想象_QUESTIONS,
  '概率统计': 概率统计_QUESTIONS,
  '排列组合': 排列组合_QUESTIONS,
  '数学游戏': 数学游戏_QUESTIONS,
  '不等式': 不等式_QUESTIONS,
  '函数基础': 函数基础_QUESTIONS,


  // 初中数学题型
  '一元一次方程': 一元一次方程_QUESTIONS,
  '二元一次方程组': 二元一次方程组_QUESTIONS,
  '二次函数': 二次函数_QUESTIONS,
  '几何证明': 几何证明_QUESTIONS,
  '函数综合': 函数综合_QUESTIONS,
  '相似三角形': 相似三角形_QUESTIONS,
  '圆的性质': 圆的性质_QUESTIONS,
  '解直角三角形': 解直角三角形_QUESTIONS,
  '统计与概率': 统计与概率_QUESTIONS,
  '反比例函数': 反比例函数_QUESTIONS,
  '代数综合': 代数综合_QUESTIONS,
  '几何综合': 几何综合_QUESTIONS,
  '应用题综合': 应用题综合_QUESTIONS,
  '动点问题': 动点问题_QUESTIONS,
  '中考专题': 中考专题_QUESTIONS,
  '数据分析': 数据分析_QUESTIONS,
  '方差标准差': 方差标准差_QUESTIONS,
  '回归分析': 回归分析_QUESTIONS,
  '竞赛基础': 竞赛基础_QUESTIONS,
  '数学建模': 数学建模_QUESTIONS,
  '实际应用': 实际应用_QUESTIONS,
  '综合压轴题': 综合压轴题_QUESTIONS,
  // 以下知识点暂时为空，待后续添加题目
  // '方程与方程组': [],
  // '函数最值': [],
  // '代数方程': [],
  // '数论进阶': [],
  // '初中概率统计': [],
  // '计数组合': [],
  // '平面几何': [],
  // '解析几何': [],
  // '应用综合': [],
  // '竞赛专题': []
};

// 获取所有题目
export const ALL_QUESTIONS = Object.values(QUESTION_BANK).flat();

// 按难度分类的题目
export const QUESTIONS_BY_DIFFICULTY = {
  1: ALL_QUESTIONS.filter(q => q.difficulty === 1),
  2: ALL_QUESTIONS.filter(q => q.difficulty === 2),
  3: ALL_QUESTIONS.filter(q => q.difficulty === 3),
  4: ALL_QUESTIONS.filter(q => q.difficulty === 4),
  5: ALL_QUESTIONS.filter(q => q.difficulty === 5),
};

// 小学题目
export const PRIMARY_QUESTIONS = [
  ...鸡兔同笼_QUESTIONS,
  ...和差倍问题_QUESTIONS,
  ...年龄问题_QUESTIONS,
  ...平均数问题_QUESTIONS,
  ...行程问题_QUESTIONS,
  ...工程问题_QUESTIONS,
  ...盈亏问题_QUESTIONS,
  ...植树问题_QUESTIONS,
  ...数论基础_QUESTIONS,
  ...几何计算_QUESTIONS,
  ...计数原理_QUESTIONS,
  ...分数应用题_QUESTIONS,
  ...小数应用题_QUESTIONS,
  ...百分数应用题_QUESTIONS,
  ...比例应用题_QUESTIONS,
  ...容斥原理_QUESTIONS,
  ...逻辑推理_QUESTIONS,
  ...抽屉原理_QUESTIONS,
  ...数字谜题_QUESTIONS,
  ...周期问题_QUESTIONS,
  ...归一问题_QUESTIONS,
  ...牛吃草问题_QUESTIONS,
  ...时钟问题_QUESTIONS,
  ...日历问题_QUESTIONS,
  ...图形计数_QUESTIONS,
  ...找规律_QUESTIONS,
  ...数列问题_QUESTIONS,
  ...面积计算_QUESTIONS,
  ...体积计算_QUESTIONS,
  ...空间想象_QUESTIONS,
  ...概率统计_QUESTIONS,
  ...排列组合_QUESTIONS,
  ...数学游戏_QUESTIONS,
  ...不等式_QUESTIONS,
  ...函数基础_QUESTIONS,
];

// 初中题目
export const MIDDLE_QUESTIONS: Question[] = [
  ...一元一次方程_QUESTIONS,
  ...二元一次方程组_QUESTIONS,
  ...二次函数_QUESTIONS,
  ...相似三角形_QUESTIONS,
  ...几何证明_QUESTIONS,
  ...函数综合_QUESTIONS,
  ...圆的性质_QUESTIONS,
  ...解直角三角形_QUESTIONS,
  ...统计与概率_QUESTIONS,
  ...反比例函数_QUESTIONS,
  ...代数综合_QUESTIONS,
  ...几何综合_QUESTIONS,
  ...应用题综合_QUESTIONS,
  ...动点问题_QUESTIONS,
  ...中考专题_QUESTIONS,
  ...数据分析_QUESTIONS,
  ...方差标准差_QUESTIONS,
  ...回归分析_QUESTIONS,
  ...竞赛基础_QUESTIONS,
  ...数学建模_QUESTIONS,
  ...实际应用_QUESTIONS,
  ...综合压轴题_QUESTIONS,
];

// 题库统计信息
export const QUESTION_STATISTICS = {
  total: ALL_QUESTIONS.length,
  byDifficulty: {
    1: QUESTIONS_BY_DIFFICULTY[1].length,
    2: QUESTIONS_BY_DIFFICULTY[2].length,
    3: QUESTIONS_BY_DIFFICULTY[3].length,
    4: QUESTIONS_BY_DIFFICULTY[4].length,
    5: QUESTIONS_BY_DIFFICULTY[5].length,
  },
  byTopic: Object.fromEntries(
    Object.entries(QUESTION_BANK).map(([topic, questions]) => [topic, questions.length])
  ),
  primary: PRIMARY_QUESTIONS.length,
  middle: MIDDLE_QUESTIONS.length,
};

// 工具函数：根据知识点获取题目
export function getQuestionsByTopic(topic: string): Question[] {
  return QUESTION_BANK[topic] || [];
}

// 工具函数：根据难度获取题目
export function getQuestionsByDifficulty(difficulty: 1 | 2 | 3 | 4 | 5): Question[] {
  return QUESTIONS_BY_DIFFICULTY[difficulty] || [];
}

// 工具函数：随机获取题目
export function getRandomQuestions(count: number, topic?: string, difficulty?: 1 | 2 | 3 | 4 | 5): Question[] {
  let questions = ALL_QUESTIONS;

  if (topic) {
    questions = getQuestionsByTopic(topic);
  }

  if (difficulty) {
    questions = questions.filter(q => q.difficulty === difficulty);
  }

  // 随机打乱并取前count个
  const shuffled = [...questions].sort(() => Math.random() - 0.5);
  return shuffled.slice(0, count);
}

// 工具函数：获取题目统计信息
export function getTopicStatistics(topic: string) {
  const questions = getQuestionsByTopic(topic);
  return {
    total: questions.length,
    byDifficulty: {
      1: questions.filter(q => q.difficulty === 1).length,
      2: questions.filter(q => q.difficulty === 2).length,
      3: questions.filter(q => q.difficulty === 3).length,
      4: questions.filter(q => q.difficulty === 4).length,
      5: questions.filter(q => q.difficulty === 5).length,
    }
  };
}

// 导出默认题库
export default QUESTION_BANK;
